Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@ambassify/throttle
Advanced tools
Throttle depending on function arguments.
npm install --save @ambassify/throttle
const throttle = require('@ambassify/throttle');
const throttledFunction = throttle(<function-to-throttle>, <timeout>, [<cache-key-resolver> | <options>]);
throttledFunction.clear(<...args>);
timeout
period for the same cache-key
(by default the first argument to this function).function-to-throttle
will be allowed with the same cache-key
(in miliseconds).cache-key
used as index into the cache. The resolver receives all of the same arguments as function-to-throttle
. Default: The value of the first argument
.cache-key-resolver
argumentMap
method interface of clear
, delete
, get
, has
, and set
.maxSize
value.true
will not cache promises resulting in rejection.cache-key-resolver
to resolve the cache key to remove from cache.CacheItems are exposed through the onCached
callback that can be specified in the throttle options.
const throttle = require('@ambassify/throttle');
let example = 0;
function myFunction(input) {
// Do some slow operation with a different result based on input
example += input;
return example;
}
// Allow `myFunction` to be called once every 2 seconds for each different `input`.
const myThrottledFunction = throttle(myFunction, 2000);
myThrottledFunction(1); // 1
myThrottledFunction(1); // 1
myThrottledFunction(1); // 1
// Wait for 2 seconds
myThrottledFunction(1); // 2
myThrottledFunction(1); // 2
myThrottledFunction(1); // 2
const conditionalThrottleFunction = throttle(myFunction, 2000, {
onCached: function(item) {
// Only cache results for large values of input
if (item.key < 10)
item.clear();
}
}
conditionalThrottleFunction(1); // 1
conditionalThrottleFunction(1); // 2
conditionalThrottleFunction(1); // 3
conditionalThrottleFunction(20); // 23
conditionalThrottleFunction(20); // 23
This library currently provides one custom caching implementation you can use for the cache
option.
This cache implementation lets you specifiy a maximum size and will evict the least recently used items in cache when the cache overflows.
const throttle = require('@ambassify/throttle');
const LruCache = require('@ambassify/throttle/cache/lru');
function myFunction(input) {}
/**
* Allow `myFunction` to be called once every 2 seconds for each different `input`
* unless it is called more than 20 times with different `input` values. In that
* case, it drops the cache item for the least recently used `input`
*/
const myThrottledFunction = throttle(myFunction, 2000, {
cache: new LruCache({ maxSize: 20 })
});
If you have some issue or code you would like to add, feel free to open a Pull Request or Issue and we will look into it as soon as we can.
We are releasing this under a MIT License.
If you would like to know more about us, be sure to have a look at our website, or our Twitter accounts @Ambassify, Sitebase, JorgenEvens
FAQs
Throttle depending on function arguments.
The npm package @ambassify/throttle receives a total of 68 weekly downloads. As such, @ambassify/throttle popularity was classified as not popular.
We found that @ambassify/throttle demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.